package cn.net.qinling.healthcare.test.datagen;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

import cn.net.qinling.healthcare.CDARootIdentifiers;

public class ExcelFileSelector implements Selector {

	private HSSFWorkbook wb = null;

	private Map<String, HSSFSheet> sheets = null;

	private Random rand = new Random();
	
	private static volatile Map<String, ExcelFileSelector> selectorMap = new HashMap<String, ExcelFileSelector>();
	
	public static Selector getSelector(String filePath) {
		if (!selectorMap.containsKey(filePath)) {
			synchronized(ExcelFileSelector.class) {
				ExcelFileSelector selector = new ExcelFileSelector(filePath);
				selectorMap.put(filePath, selector);
			}
		}
		
		return selectorMap.get(filePath);
	}

	private ExcelFileSelector(String filePath) {
		File file = new File(filePath);
		try {
			FileInputStream fis = new FileInputStream(file);
			wb = new HSSFWorkbook(new POIFSFileSystem(fis));

			sheets = new HashMap<String, HSSFSheet>();
		} catch (IOException ex) {
			ex.printStackTrace();
			wb = null;
		}
	}

	private HSSFSheet getSheet(String sheetName) {
		if (null == wb) {
			return null;
		}

		HSSFSheet sheet = sheets.get(sheetName);
		if (null == sheet) {
			sheet = wb.getSheet(sheetName);
			sheets.put(sheetName, sheet);
		}

		return sheet;

	}

	/* (non-Javadoc)
	 * @see cn.net.qinling.healthcare.test.datagen.Selector#next(java.lang.String)
	 */
	@Override
	public List<String> next(String category) {
		HSSFSheet sheet = getSheet(category);
		if (null == sheet) {
			return null;
		}

		List<String> result = new ArrayList<String>();
		int rowNum = sheet.getLastRowNum();
		int randIndex = rand.nextInt(rowNum);
		HSSFRow randRow = sheet.getRow(randIndex + 1);
		Iterator<Cell> cellIter = randRow.cellIterator();
		while (cellIter.hasNext()) {
			Cell cell = cellIter.next();
			String strExcelCell = "";
			switch (cell.getCellType()) {
			case HSSFCell.CELL_TYPE_FORMULA:
				strExcelCell = "FORMULA ";
				break;
			case HSSFCell.CELL_TYPE_NUMERIC: {
				strExcelCell = String.valueOf((int)cell.getNumericCellValue());
			}
				break;
			case HSSFCell.CELL_TYPE_STRING:
				strExcelCell = cell.getStringCellValue();
				break;
			case HSSFCell.CELL_TYPE_BLANK:
				strExcelCell = "";
				break;
			default:
				strExcelCell = "";
				break;
			}
			result.add(strExcelCell);
		}

		return result;
	}

	public static void main(String[] args) {
		Selector selector = ExcelFileSelector.getSelector("e:\\codes.xls");
		for (int i = 0; i < 100; i++) {
			List<String> result = selector
					.next(CDARootIdentifiers.HCPROVIDER_ORGANIZATION);
			for (String str : result) {
				System.out.println(str);
			}
		}
	}

}
